package verfify;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * Created by zhaopeng on 2/14/17.
 */
public class VerifyMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
    private Text key = new Text();
    private IntWritable value = new IntWritable();
    private Map<String, String> mapper = new HashMap<String, String>();
    @Override
    public void map(LongWritable keys, Text values, Context context) throws IOException, InterruptedException {
        String line = values.toString();
        mapper.clear();
        getMap(line);
        if(0==mapper.size())
            return;

         key.set(mapper.get("token"));

        if("{}".equals(mapper.get("result")))
            value.set(0);
        else
            value.set(1);

        context.write(key, value);
    }
    private void getMap(String line) {
        String[] args = line.split(Pattern.quote("^"));
        for (String param : args) {
            String[] tmp = param.split("=");
            if (2 != tmp.length)
                continue;

            if ("token".equals(tmp[0]) || "result".equals(tmp[0]))
                mapper.put(tmp[0], tmp[1]);
        }
    }
}
